package com.bRoom.service.impl;

import com.bRoom.common.Conts;
import com.bRoom.common.ForgetToken;
import com.bRoom.common.ServerResponse;
import com.bRoom.dao.UserMapper;
import com.bRoom.pojo.User;
import com.bRoom.service.IUserService;
import com.bRoom.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.UUID;


@Service("iUserService")
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper  userMapper;

    @Override
    public ServerResponse<User> login(String userName, String userPassword) {
        if (StringUtils.isEmpty(userName)){
             return ServerResponse.createByErrorMsg("用户名不能为空");
        }
        int checkUsername=userMapper.checkUsername(userName);

        if (checkUsername==0){
             return ServerResponse.createByErrorMsg("用户名不存在");
        }

        String Md5Password= MD5Util.MD5EncodeUtf8(userPassword);
        User user=userMapper.login(userName,Md5Password);
        if (user==null){
             return ServerResponse.createByErrorMsg("密码错误");
        }
        return ServerResponse.createBySuccessMsgData("登录成功",user);
    }

    public  ServerResponse<User> register(User user){
        if (user==null){
             return ServerResponse.createByErrorMsg("参数错误");
        }
          user.setUserRole(Conts.ROLE.ROLE_USER);
          user.setUserPassword(MD5Util.MD5EncodeUtf8(user.getUserPassword()));
        int register=userMapper.insert(user);
        if (register==0){
             return ServerResponse.createByErrorMsg("注册失败");
        }
        return ServerResponse.createBySuccessMsg("注册成功");
    }

    public ServerResponse<User> updateMessage(User user,Integer userId){
          int checkUserName=userMapper.checkUpdateUsername(user.getUserName(),userId);
          if (checkUserName>0){
               return ServerResponse.createByErrorMsg("用户名已存在");
          }

          User updateUser=new User();
          updateUser.setUserId(userId);
          updateUser.setUserName(user.getUserName());
          updateUser.setUserTel(user.getUserTel());
          updateUser.setQuestion(user.getQuestion());
          updateUser.setAnswer(user.getAnswer());
          int updateUserCount=userMapper.updateByPrimaryKeySelective(updateUser);
          if (updateUserCount==0){
               return ServerResponse.createByErrorMsg("信息修改失败");
          }
          return ServerResponse.createBySuccessMsg("信息修改成功");
    }


    public ServerResponse findQuestion(String userName){

        int checkUsername=userMapper.checkUsername(userName);
        if (checkUsername==0){
             return ServerResponse.createByErrorMsg("找不到相关用户，请重新输入");
        }
         String question=userMapper.findQuestion(userName);
         if (org.apache.commons.lang3.StringUtils.isNotBlank(question)){
              return ServerResponse.createBySuccessData(question);
         }
        return ServerResponse.createByErrorMsg("该用户下没有找回密码的问题");
    }

      public ServerResponse findAnswer(String userName,String question,String answer){
           int checkAnswer=userMapper.checkAnswer(userName,question,answer);
           if (checkAnswer>0){
                String forgetToken=UUID.randomUUID().toString();
                ForgetToken.setKey(ForgetToken.FORGET_TOKEN+userName,forgetToken);
                return ServerResponse.createBySuccessData(forgetToken);
           }
           return ServerResponse.createByErrorMsg("答案错误");
      }

    public ServerResponse forgetPasswordResert(String userName,String passwordNew,String forgetToken){
        if (org.apache.commons.lang3.StringUtils.isBlank(forgetToken)){
             return ServerResponse.createByErrorMsg("参数错误");
        }
        int checkUserName=userMapper.checkUsername(userName);
        if (checkUserName==0){
             return ServerResponse.createByErrorMsg("用户不存在");
        }
        String token=ForgetToken.getKey(ForgetToken.FORGET_TOKEN+userName);
        if (org.apache.commons.lang.StringUtils.isBlank(token)){
             return ServerResponse.createByErrorMsg("token无效，过期");
        }
        if (org.apache.commons.lang3.StringUtils.equals(forgetToken,token)){
              String Md5Password=MD5Util.MD5EncodeUtf8(passwordNew);
              int resertCount=userMapper.resertPassword(userName,Md5Password);
              if (resertCount>0){
                   return ServerResponse.createBySuccessMsg("修改密码成功");
              }
              return ServerResponse.createByErrorMsg("修改密码失败");
        }
        return ServerResponse.createByErrorMsg("token错误，请重新获取");
    }

    public  ServerResponse loginResertPassword(String passWordOld,String passWordNew,Integer userId){
          int checkPassword=userMapper.checkPassword(MD5Util.MD5EncodeUtf8(passWordOld),userId);
          if (checkPassword>0){
                 String Md5Password=MD5Util.MD5EncodeUtf8(passWordNew);
                 int rowCount=userMapper.loginResertPAssword(Md5Password,userId);
                 if (rowCount>0){
                      return ServerResponse.createBySuccessMsg("修改密码成功");
                 }
                 return ServerResponse.createByErrorMsg("修改密码失败");
          }
          return ServerResponse.createByErrorMsg("旧密码错误，请重新输入");
    }
}
